# This file is part of OrangeSpider. OrangeSpider is a meta model based
# development tool.
# 
# OrangeSpider is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# OrangeSpider is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with OrangeSpider.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2009 Stefan Nikolaus <OrangeSpiderMaster@googlemail.com>

from MetaModel import *
import Ui
import FastEdit
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import Component
import math
import random
import Persistence
import Kernel
import Project

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)
        self.setWindowTitle("Monolithic Spider")

        self.fileMenu = None
        self.dataMenu = None
        self.extraMenu = None

        self.fileMenu = self.menuBar().addMenu("&File")
        self.dataMenu = self.menuBar().addMenu("&Data")
        self.extraMenu = self.menuBar().addMenu("&Extra")

        self.fileOpenAction = QAction("&Open", self)
        self.connect(self.fileOpenAction, SIGNAL("triggered()"),
                     self.fileOpen)
        self.fileMenu.addAction(self.fileOpenAction)

        self.fileCreateAction = QAction("&Create", self)
        self.connect(self.fileCreateAction, SIGNAL("triggered()"),
                     self.fileCreate)
        self.fileMenu.addAction(self.fileCreateAction)

        self.fileDeleteAction = QAction("&Delete", self)
        self.connect(self.fileDeleteAction, SIGNAL("triggered()"),
                     self.fileDelete)
        self.fileMenu.addAction(self.fileDeleteAction)

        self.fileMoveAction = QAction("&Move", self)
        self.connect(self.fileMoveAction, SIGNAL("triggered()"),
                     self.fileMove)
        self.fileMenu.addAction(self.fileMoveAction)

        self.fileCloseAction = QAction("&Close", self)
        self.connect(self.fileCloseAction, SIGNAL("triggered()"),
                     self.fileClose)
        self.fileMenu.addAction(self.fileCloseAction)

        self.dataComponentAction = QAction("&ComponentManager Data", self)
        self.connect(self.dataComponentAction, SIGNAL("triggered()"),
                     self.dataComponent)
        self.dataMenu.addAction(self.dataComponentAction)

        self.dataPackageManagerAction = QAction("&PackageManagerManager Data", self)
        self.connect(self.dataPackageManagerAction, SIGNAL("triggered()"),
                     self.dataPackageManager)
        self.dataMenu.addAction(self.dataPackageManagerAction)

        pix = QPixmap("img/spider.png")
        pixLabel = QLabel()
        pixLabel.setPixmap(pix)
        self.setCentralWidget(pixLabel)
        self.setWindowIcon(QIcon("img/spiderIco.png"))
        
    def fileOpen(self):
        filename = QFileDialog.getOpenFileName(self, "Open project", ".", "*.spid")
        if filename <> "":
            projectComp = Kernel.componentManager.createComponent("Spider.Project.Project")
            projectComp.load(str(filename))
            projectComp.show()
            
    def fileDelete(self):
        filename = QFileDialog.getOpenFileName(self, "Delete project", ".", "*.spid")
        if filename <> "":
            if QMessageBox.Ok == QMessageBox.critical(None, "Spider", "Delete really?"):
                projectComp = Project.ProjectComponent()
                projectComp.deleteProject(str(filename))

    def fileCreate(self):
        filename = QFileDialog.getExistingDirectory(self, "Create project", ".")
        if filename <> "":
            newName = self._getName()
            if newName != "":
                projectComp = Kernel.componentManager.createComponent("Spider.Project.Project")
                f1 = projectComp.create(str(filename), newName)
                projectComp.show()

    def fileMove(self):
        pass
    def fileClose(self):
        self.hide()
        sys.exit(0)
    def dataComponent(self):
        r = Kernel.componentManager.repository
        mb = Ui.MatrixWidget(r)
        mb.exec_()

    def dataPackageManager(self):
        r = Kernel.packageManager.repository
        mb = Ui.MatrixWidget(r)
        mb.exec_()
    
    def _getName(self):
        nd = Project.NameDlg(self)
        if nd.exec_():
            return str(nd.editField.text())
        return ""
    
if __name__ == "__main__":
    sys.path.append("./Packages")

    app = QApplication(sys.argv)

    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

    sys.exit(0)
